package cn.credit.loan.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import cn.credit.loan.entity.Grant;
import cn.credit.loan.service.grant.GrantService;
/**
 * 发放员自定义类
 * @author Administrator
 *
 */
public class GrantReaml extends AuthorizingRealm {

	@Autowired 
	private GrantService grantService;
	
	// 设置realm的名称
	@Override
	public void setName(String name) {
		super.setName("grantReaml");
	}
	
	/**
	 * 授权
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		
		return null;
	}
	
	/**
	 * 认证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//获取
		String granname=(String) token.getPrincipal();
		// 第二步：根据用户输入的granname从数据库查询
		Grant grant=null;
		try {
			grant=grantService.grantlogin(granname, null);
			if(grant==null){
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		String granPwd=grant.getGrantPwd();
		// 将Grant设置SimpleAuthenticationInfo
		SimpleAuthenticationInfo simpleAuthenticationInfo=new SimpleAuthenticationInfo(
				grant, 
				granPwd,
				getName());
		return simpleAuthenticationInfo;
	}

	
}
